#include <stdio.h>
#include <stdlib.h>
#include "aimage.h"
#include "aimage_utils.h"
#include "aimage_ppm.h"

int main( int argc, char *argv[] )
{
	AIMAGE image;
	float rg, gg, bg;

	if ( argc != 1 && argc != 2 )
	{
		fprintf(stderr, "%s [in_file]\n", argv[0] );
		exit( 1 );
	}

	if ( argc == 2 )
	{
		fprintf(stderr,"grid reading from %s\n", argv[1]);
		AIMAGE_PPM_UTILS::load( image, argv[1] );
	}
	else
	{
		fprintf(stderr,"grid reading from stdin\n");
		AIMAGE_PPM_UTILS::load( image, "stdin" );
	}

	int xd, yd;
	image.get_dim( xd, yd );
	fprintf(stderr,"grid done read file dim %d by %d\n", xd, yd);

	APIXEL c( 0.5, 0.5, 1.0 );
	for ( int x = 0; x < xd; x+=20 )
	{
		int x0, x1;
		x0 = x;
		x1 = x + 19;
		if ( x1 >= xd ) x1 = xd-1;
		image.line( x0, 0, x0, yd-1, c );
		image.line( x1, 0, x1, yd-1, c );
	}
	for ( int y = 0; y < yd; y+=20 )
	{
		int y0, y1;
		y0 = y;
		y1 = y + 19;
		if ( y1 >= yd ) y1 = yd-1;
		image.line( 0, y0, xd-1, y0, c );
		image.line( 0, y1, xd-1, y1, c );
	}

	fprintf(stderr,"grid writing file\n");

	AIMAGE_PPM_UTILS::save( image, "stdout" );
}

